#region Using directives

using System;
using System.IO;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Forms;
using System.Windows.Forms;

#endregion
/** 
COPYRIGHT (C) 2008 Piyusha Desai. All Rights Reserved.
* Read Licence.txt before using this application.
*/
namespace SmsBuddy
{

 // All property of group is here
 
    public class Group
    {
        int id;
     
       
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        string name;
  
   
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        bool sendMeetingInfo;
      
            
        public bool SendMeetingInfo
        {
            get { return sendMeetingInfo; }
            set { sendMeetingInfo = value; }
        }

        bool sendMessage;
   
             
        public bool SendMessage
        {
            get { return sendMessage; }
            set { sendMessage = value; }
        }

        string message;
         
      
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        bool enabled;


        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        string image = "";
    
             
        public string Image
        {
            get { return image; }
            set { image = value; }
        }

         public Group()
        {
        }
        
      
        public void Save(StreamWriter sw)
        {
            sw.Write(Id);
            sw.Write('\t');
            sw.Write(Enabled);
            sw.Write('\t');
            sw.Write(Name);
            sw.Write('\t');
            sw.Write(SendMeetingInfo);
            sw.Write('\t');
            sw.Write(SendMessage);
            sw.Write('\t');
            sw.Write(Message);
            sw.Write('\t');
            sw.Write(Image);
            sw.WriteLine();
        }

 
      
        public static Group Load(StreamReader sr)
        {
            Group group = null;
            string line = sr.ReadLine();
                     
            if (line != null)
            {
                group = new Group();
                string[] data = line.Split('\t');
                group.Id = int.Parse(data[0]);
                group.Enabled = bool.Parse(data[1]);
                group.Name = data[2];
                group.SendMeetingInfo = bool.Parse(data[3]);
                group.SendMessage = bool.Parse(data[4]);
                group.Message = data[5];

                if (data.Length >= 7)
                {
                    group.Image = data[6];
                }
            }

            return group;
        }

  
     
        public override string ToString()
        {
            return Name;
        }

        public void AddContact(Contact contact)
        {
            #region Custom Property
            // We store the id of the group in a custom 
            // property 'Buddy.ContactPropertyName'.  This
            // allows a very quick lookup of which group the user is in. 

            contact.Properties[Buddy.ContactPropertyName] = Id;

            // data isn't saved until we call update
            contact.Update();
            #endregion
        }            
      
        public Contact AddContact()
        {
            Contact contact = null;

            #region ChooseContactDialog

            // This method uses the ChooseContactDialog to allow
            // the user to pick an available contact.
            ChooseContactDialog dlg = new ChooseContactDialog();

            // we only want the contact, so we set ChooseContactOnly
            // to true
            dlg.ChooseContactOnly = true;

            // We only want to show contacts that have a valid SMS property,
            // so we use the RequiredProperties property to filter on 
            // ContactProperty.Sms
            dlg.RequiredProperties = new ContactProperty[] { ContactProperty.Sms };

            // show the ChooseContactDialog
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                contact = dlg.SelectedContact;

                // if a contact was selected
                if (contact != null)
                {
                    // add the contact to the group
                    AddContact(contact);
                }
            }
            #endregion

            return contact;
        }
             
      
      
        public ContactCollection GetContacts()
        {
            ContactCollection contacts = null;

         
            using (OutlookSession session = new OutlookSession())
            {
                if (session.Contacts.Items.Count > 0)
                {
                
                    bool propertyCreated = 
                        session.Contacts.Items[0].Properties.Contains(Buddy.ContactPropertyName);

                    if (propertyCreated)
                    {
                           contacts =
                            session.Contacts.Items.Restrict("[" + Buddy.ContactPropertyName + "] = " + Id + "");
                    }
                }
            }
            return contacts;
        }

           
        public void RemoveAllContacts()
        {
            ContactCollection contacts = GetContacts();
            if (contacts != null)
            {
                foreach (Contact contact in contacts)
                {
                    contact.Properties[Buddy.ContactPropertyName] = 0;

                 contact.Update();
                }
            }
        }
    }
}
